#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDir>

// QQmlContext to operate Qml Context
#include <QQmlContext>
#include "LocalDirIterator.h"
#include <QStandardPaths> //mac need
#include <QDebug>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);

    // construct a LocalDirIterator,
    QString path(QDir::currentPath());

//adapt the image path according to running platform
#ifdef _WIN32
    // Windows (32-bit and 64-bit)
    path += "/../image";
#elif __APPLE__
    // Mac
    path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/image";//equal to: path = QDir::homePath()+"/image";
#elif __linux__
    // linux
    path += "/../image";
#else
#error "Unknown SYSTEM"
#endif


    if (argc == 2) {
        path = argv[1];
    }
    LocalDirIterator dirIterator(&app, path);
    
    // pass LocalDirIterator to engine
    engine.rootContext()->setContextProperty("LocalDirIterator", &dirIterator);
    
    engine.load(url);
    return app.exec();
}
